package elijah.base.net;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

import java.util.concurrent.TimeUnit;

import elijah.base.dialog.BasePopupCallback;
import elijah.base.thread.ScheduledProvider;
import elijah.base.thread.Threads;
import elijah.base.utils.CommonUtils;
import elijah.base.utils.DialogUtils;
import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.SingleObserver;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

abstract class BaseProgressObserver<T> implements SingleObserver<T> {
    private boolean showDialog = true;
    private BasePopupView dialog;
    private Context context;
    private String content;
    private boolean fullWindow = true;
    private final CompositeDisposable disposable = new CompositeDisposable();

    public BaseProgressObserver() {
        this(null, true, true, null);

    }

    public Context getObserverContext() {
        return context == null ? ScheduledProvider.instance().getCurrentActivity() : context;
    }

    public BaseProgressObserver(Context context) {
        this(context, true, true, null);
    }

    public BaseProgressObserver(boolean showDialog) {
        this(null, showDialog, true, null);
    }

    public BaseProgressObserver(String content) {
        this(null, true, true, content);
    }

    public BaseProgressObserver(Context context, String content) {
        this(context, true, true, content);

    }

    public BaseProgressObserver(Context context, boolean showDialog) {
        this(context, showDialog, true);


    }

    public BaseProgressObserver(Context context, boolean showDialog, boolean fullWindow) {
        this(context, showDialog, fullWindow, null);


    }

    public BaseProgressObserver(Context context, boolean showDialog, boolean fullWindow, String content) {
        this.context = context;
        this.content = content;
        this.showDialog = showDialog;
        this.fullWindow = fullWindow;



    }

    public void customerDialog(XPopup.Builder dialogBuilder) {

    }


    @Override
    public void onSubscribe(final Disposable d) {
        disposable.add(d);
        if (showDialog) {
            disposable.add(Completable.complete()
                    .compose(Threads.io2main())
                    .subscribe(() -> {
                        XPopup.Builder dialogBuilder = new XPopup.Builder(context)
                                .dismissOnTouchOutside(false)
                                .hasStatusBar(!fullWindow)
                                .hasNavigationBar(!fullWindow)
                                .dismissOnBackPressed(true)
                                .setPopupCallback(new BasePopupCallback() {
                                    @Override
                                    public void onDismiss(BasePopupView popupView) {
                                        disposable.dispose();
                                    }
                                });
                        customerDialog(dialogBuilder);
                        dialog = dialogBuilder.asLoading(TextUtils.isEmpty(content) ? "加载中" : content)
                                .show();
                    }));

        }

    }

    @Override
    public void onSuccess(T t) {
        if (showDialog) {
            disposable.add(Completable.complete()
                    .compose(Threads.io2main())
                    .subscribe(() -> DialogUtils.dismiss(dialog)));

        }
    }

    @Override
    public void onError(Throwable e) {
        if (showDialog) {
            disposable.add(Completable.complete()
                    .compose(Threads.io2main())
                    .subscribe(() -> DialogUtils.dismiss(dialog)));
        }
        error(e);

    }


    public abstract void error(Throwable e);


}
